package cn.Mmovie.servlet;

import cn.Mmovie.bean.Cinema;
import cn.Mmovie.dao.CinemaDao;
import cn.Mmovie.dao.Impl.CinemaDaoImpl;
import cn.Mmovie.service.CinemaService;
import cn.Mmovie.service.impl.CinemaServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "AdminCinemaAddServlet", value = "/admin/cinema_add")
public class AdminCinemaAddServlet extends HttpServlet {
    private CinemaService cinemaService=new CinemaServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String cinemaName=request.getParameter("cinema_name");
        String location=request.getParameter("location");
        Integer capacity=Integer.parseInt(request.getParameter("capacity"));
        Cinema c=new Cinema(cinemaName,location,capacity);

        Integer res=cinemaService.addCinema(c);
        if (res>0){
            request.setAttribute("msg","电影院添加成功！");
            request.getRequestDispatcher("/admin/cinema_list").forward(request,response);
        }else if (res==-1){
            request.setAttribute("failMsg","电影院名已存在，请重试");
            request.setAttribute("cinema",c);
            request.getRequestDispatcher("/admin/add_goods").forward(request,response);
        }else {
            request.setAttribute("failMsg","电影院添加失败！");
            request.setAttribute("cinema",c);
            request.getRequestDispatcher("/admin/add_goods").forward(request,response);
        }
    }
}
